﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VQData;
using System.Data;
using VQWEB.common;
using System.Data.Linq;

namespace VQWEB.administrator.sources
{
    public class ProductData
    {
        public ProductData() { }
        #region product
        //add edit
        public int InsertOrUpdateProduct(core_product obj) {
           try
            {
                int Id = 0;
                using (CoreModelDataContext context = new CoreModelDataContext())
                {
                    if (obj.product_id > 0)
                    {
                        core_product flag = GetProductById(obj.product_id);                        
                        if (flag != null)
                        {
                            context.core_products.Attach(obj);
                            context.Refresh(RefreshMode.KeepCurrentValues, obj);
                            context.SubmitChanges();
                            Id = obj.product_id;
                        }
                    }
                    else
                    {
                        context.core_products.InsertOnSubmit(obj);
                        context.SubmitChanges();
                        Id = obj.product_id;
                        
                    }
                   
                }
                return Id;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }  
        }
        //LockUnlock
        public void LockUnlockProduct(List<int> array_id)
        {

            using (CoreModelDataContext context = new CoreModelDataContext())
            {
                var query = from p in context.core_products
                            where array_id.Contains(p.product_id)
                            select p;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.product_active= (item.product_active == true) ? false : true;
                        context.SubmitChanges();
                    }
                }
            }
        }
        //Delete
        public void Delete_Produc(List<int> array_id)
        {
            using (CoreModelDataContext context = new CoreModelDataContext())
            {
                var query = from p in context.core_products
                            where array_id.Contains(p.product_id)
                            select p;
                if (query.Any())
                {
                    context.core_products.DeleteAllOnSubmit(query);
                    context.SubmitChanges();
                }
            }
        }
        //View Category by Id
        public core_product GetProductById(int id)
        {
            using (CoreModelDataContext context = new CoreModelDataContext())
            {
                var Obj = context.core_products.Where(p => p.product_id == id).SingleOrDefault();
                if (Obj != null)
                    return Obj;
                else
                    return null;
            }
        }
        //List View core_product
        public DataTable ViewProduct()
        {
            using (CoreModelDataContext context = new CoreModelDataContext())
            {
                var query = from p in context.core_products
                            select new
                            {
                                product_id = p.product_id,
                                product_title = p.product_title,
                                //category_name = p.core_product_category.category_name,
                                product_description = p.product_description,
                                product_content = p.product_content,
                                product_date= p.product_date,
                                product_hit = p.product_hit,
                                product_img = p.product_img,                               
                                product_active = p.product_active
                            };
                if (query.Any())
                    return HelperData.LINQToDataTable(query);
                return new DataTable();
            }
        }
        #endregion        
    }
}